<?php
/**
 *搜索时间限制
 * User: Tommy
 * Date: 2018/01/02
 * Time: 10:28
 */

namespace App\Services;


use Carbon\Carbon;
use common\utils\SysCode;

class SearchTimeLimitService
{
    //限制时间月为单位
    const ONE_MONTH = '1';
    const TWO_MONTH = '2';
    const THREE_MONTH = '3';
    const SIX_MONTH = '6';

    public static function timeLimit($startDate, $endDate, $limitDate)
    {
        $startDate      = date('Y-m', strtotime($startDate));//时间限制以月为单位，所以先取开始时间的年月计算
        $timeDifference = Carbon::parse($startDate)->addMonths($limitDate)->toDateTimeString();
        $timeDifference = strtotime($timeDifference);
        $endTime        = strtotime(date('Y-m', strtotime($endDate)));//时间限制以月为单位，所以先取结束时间的年月计算
        if ($timeDifference < $endTime) {
            error_exit(SysCode::SEARCH_TIMEL_IMIT_ERROR, ['month' => $limitDate]);
        }
    }
}